package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.configuration.Constant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.layout.ItemGroup;
import com.example.capsule_chat0322.request.UserInfoRequest;

/**
 * @Author zhuzhiming
 * @Date 2022/5/17 20:00
 */
public class OneChatRecordActivity extends AppCompatActivity {

    private MarqueTextView marqueTextView;
    private ItemGroup user_id;
    private ItemGroup user_nickname;
    private ItemGroup user_sign;
    private ItemGroup user_sex;
    private ItemGroup user_age;
    private ItemGroup user_hobby;
    private ItemGroup user_email;
    private ImageView user_avatar;
    private TextView recent_chat;
    private LinearLayout content_liner;


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one_chat_record);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initView() {

        marqueTextView = findViewById(R.id.send_content);
        marqueTextView.setSelected(true);
        user_id = findViewById(R.id.user_mark);
        user_nickname = findViewById(R.id.user_nickname);
        user_avatar = findViewById(R.id.user_avatar);
        user_sign = findViewById(R.id.user_sign);
        user_sex = findViewById(R.id.user_sex);
        user_age = findViewById(R.id.user_age);
        user_hobby = findViewById(R.id.user_hobby);
        user_email = findViewById(R.id.user_email);
        recent_chat = findViewById(R.id.recent_chat);
        content_liner = findViewById(R.id.content_liner);

        String senderId = getIntent().getStringExtra("senderId");

        final UserInfoBean[] userInfoBean = {(UserInfoBean) GlobalAssistMap.getObject(senderId + "Info")};


        if (userInfoBean[0] == null) {
            UserInfoRequest.doGetUserInfoRequest(new Handler(new Handler.Callback() {
                @RequiresApi(api = Build.VERSION_CODES.O)
                @Override
                public boolean handleMessage(@NonNull Message message) {
                    userInfoBean[0] = (UserInfoBean) GlobalAssistMap.getObject(senderId + "Info");
                    setUserInfo(userInfoBean[0]);
                    return true;
                }
            }), senderId);
        } else {
            setUserInfo(userInfoBean[0]);
        }

    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setUserInfo(UserInfoBean userInfoBean) {
        user_id.getContentEdt().setText(String.valueOf(userInfoBean.getId()));
        user_nickname.getContentEdt().setText(userInfoBean.getNickname());
        user_age.getContentEdt().setText((userInfoBean.getAge() + "").equals("-1") ? "" : userInfoBean.getAge() + "");
        user_sex.getContentEdt().setText((userInfoBean.getSex() + "").equals("-1") ? "" : (userInfoBean.getSex() == 0 ? "女" : "男"));
        user_sign.getContentEdt().setText(userInfoBean.getSign());
        user_email.getContentEdt().setText(userInfoBean.getEmail());
        user_hobby.getContentEdt().setText(userInfoBean.getHobby());
        if (userInfoBean.getAvatarUrl().contains("/")) {
            UserInfoRequest.getAvatar(new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message message) {
                    String avatarPath = Constant.IMAGE_PATH + userInfoBean.getAvatarUrl().substring(userInfoBean.getAvatarUrl().lastIndexOf("/") + 1);
                    Bitmap bitmap = BitmapFactory.decodeFile(avatarPath);
                    user_avatar.setImageBitmap(bitmap);
                    return true;
                }
            }), userInfoBean.getAvatarServerPath());
        } else {
            user_avatar.setImageResource(Integer.parseInt(userInfoBean.getAvatarUrl()));
        }

        String roomName = getIntent().getStringExtra("roomName");
        String senderName = getIntent().getStringExtra("senderName");
        String sendTime = getIntent().getStringExtra("sendTime");
        String content = getIntent().getStringExtra("content");
        marqueTextView.setText(content);

        content_liner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("roomName", roomName);
                intent.putExtra("senderName", senderName);
                intent.putExtra("content", content);
                intent.putExtra("sendTime", sendTime);
                intent.setClass(OneChatRecordActivity.this, OneChatRecordInfoActivity.class);
                startActivity(intent);
            }
        });

        recent_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();
                intent.setClass(OneChatRecordActivity.this, RecentChatRecordActivity.class);
                intent.putExtra("roomName", roomName);
                intent.putExtra("senderName", senderName);
                intent.putExtra("content", content);
                intent.putExtra("sendTime", sendTime);
                intent.putExtra("senderId", String.valueOf(userInfoBean.getId()));
                startActivity(intent);

            }
        });

    }


}
